package com.adtech.adms.systemlog;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.apache.log4j.Logger;
import org.exolab.castor.mapping.Mapping;
import org.exolab.castor.xml.Unmarshaller;
import org.xml.sax.InputSource;

import com.adtech.adms.Constants;
import com.adtech.core.util.FileUtil;

/**
 * 系统日志辅助类
 * 加载系统日志类型
 * @author Dormin 
 * @since 2011-6-14 
 * 
 * @Copyright (C) AD Tech Workshop All Right Reserved
 */
public class SystemLogHelper {
	private static Logger log = Logger.getLogger(SystemLogHelper.class);

	private static Map<Integer,LogType> logTypeMap = new ConcurrentHashMap<Integer,LogType>();

	/**
	 * 
	 * @function 应用初始化时加载系统日志类型信息
	 * @param sc 应用上下文
	 *
	 * @return
	 * @exception
	 *
	 * @author Dormin
	 * @history
	 *
	 */
	public static void init() {
		try {
			String mappingFile = FileUtil.getRealPath(Constants.SYSTEM_LOG_MAPPING);
			String configFile = FileUtil.getRealPath(Constants.SYSTEM_LOG_CONFIG);
			Mapping mapping = new Mapping();
			mapping.loadMapping(mappingFile);
			Unmarshaller unmar = new Unmarshaller(mapping);
			InputSource input = new InputSource(configFile);
			input.setEncoding(Constants.DEFAULT_ENCODING);
			LogTypeList logTypeList = (LogTypeList) unmar.unmarshal(input);
			for(LogType logType : logTypeList.getTypeList()){
				logTypeMap.put(logType.getId(), logType);
			}
		} catch (Exception e) {
			log.error(e);
		}
	}

	/**
	 * 
	 * @function 根据日志类型id获取日志类型
	 * @param typeId 日志类型id
	 *
	 * @return 日志类型对象
	 * @exception
	 *
	 * @author Dormin
	 * @history
	 *
	 */
	public static LogType getLogType(int typeId) {
		if (logTypeMap.isEmpty()){
			init();
		}
		
		return logTypeMap.get(typeId);
	}
}
